package com.foodndate.food.events;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/event")
public class EventController {
	@Autowired private EventDao eventdao;
	
	//이벤트메인페이지
	@RequestMapping(value="/" ,method=RequestMethod.GET)
	public String main(Model model){
		model.addAttribute("list" , eventdao.listinfo()); 
		return "events/eventsHome";			
	}
	//이벤트추가페이지
	@RequestMapping(value="/addgo" ,method=RequestMethod.GET)
	public String add(){
		return "events/eventadd";			
	}
	//이벤트삭제
		@RequestMapping(value="/delete" ,method=RequestMethod.GET)
		public String delete(int num, Model model){
			eventdao.delete(num);
			model.addAttribute("list" , eventdao.listinfo());
			return "events/eventsHome";			
		}
	//이벤트추가
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String addP(EventDto eventdto) throws IOException{
		eventdao.insert(eventdto);
		//새로고침하면 계속 insert를 호출되는것을 막기위해서 redirect사용
		return "redirect:/event/";
	}
	//이벤트 상세보기
	@RequestMapping(value="/view" ,method=RequestMethod.GET)
	public String eventview(Model model, String img2){
		model.addAttribute("list" , eventdao.listinfo());
		model.addAttribute("img2" , img2);
		return "events/eventview";			
	}
	//수정
		@RequestMapping(value="/update" ,method=RequestMethod.POST)
		public String update(EventDto eventdto, int num){
			eventdao.update(eventdto, num);
			return "redirect:/event/";			
		}
		
		//수정페이지
		@RequestMapping(value="/updatego" ,method=RequestMethod.GET)
		public String qwe(Model model,String num){
			model.addAttribute("dto" , eventdao.testinfo(num)); 
			return "events/eventupdate";			
		}
}
